<%@ page pageEncoding="UTF-8"%>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<%@ taglib prefix="isperp" uri="/WEB-INF/tlds/isperp.tld"%>

<script type="text/javascript">
	var sparepart = {
		gridId : "sparepartBackDetailGrid",
		saveUrl : "sparepart/sparepartChangeDetail/save"
	};
</script>

<script type="text/javascript" src="js/widget/sparepart.js"></script>

<div id="tempDiv">

</div>

<form:form id="sparepartBackForm" commandName="model"
	cssClass="validate" action="sparepart/sparepartBack/save">
	<form:hidden path="id" />
	<table class=" form-table">
		<tr>
			<td class="td1">编号：</td>
			<td class="td2"><form:input path="code" disabled="true" /><span>
					(自动生成)</span></td>
			<td class="td1">归还日期：</td>
			<td class="td2"><form:input type="date" path="backDate"
					cssClass="required" /></td>
		</tr>
		<tr>
			<td class="td1">归还人：</td>
			<td class="td2"><form:hidden path="backManId" /> <isperp:departmentUser
					id="backManName" displayValue="${model.backManName }"
					afterSelected="afterSelected(row);" displayValueCssClass="required"
					name="backManName" valueControlId="backManId"></isperp:departmentUser>
			</td>
			<td class="td1">项目：</td>
			<td class="td2"><form:select path="projectId"
					items="${projects }" itemLabel="name" itemValue="id"
					cssClass="required search"></form:select></td>
		</tr>
		<tr>
			<td class="td1">归还类型：</td>
			<td class="td2" colspan="3">归还维修
			</td>
		</tr>
		<tr>
			<td class="td1">描述：</td>
			<td class="td2" colspan="3"><form:textarea path="memo"
					cssClass="required" /> </textarea>
			</td>
		</tr>
		<tr>
			<td class="td1">填表人：</td>
			<td class="td2"><label>${creatorName }</label></td>
		</tr>
	</table>
</form:form>

<div id="sparepartRepairFormDiv" >
	<div class="header">
		<h3>
			归还<span style='color:red'>维修</span>清单
		</h3>
	</div>
	<isperp:grid
	url="sparepart/sparepartChangeDetail/listDetails?relationId=${model.id }"
	gridId="sparepartBackDetailGrid" showPager="false" showSearchbar="false"
	cusDialogSize="big" editurl="sparepart/sparepartChangeDetail/save"
	cusDeleteUrl="sparepart/sparepartChangeDetail/delete"
	cusFormatterActions="false" showToolbar="false"  >
	<jsp:attribute name="toolbar">
		<isperp:toolbar>
		</isperp:toolbar>
		</jsp:attribute>
	     <jsp:body>
		<isperp:column name="id" title="id" hidden="true"></isperp:column>
		<isperp:column name="spt_code" title="编号" width="80">
		</isperp:column>
		<isperp:column name="treenode_name_path" title="备件分类" width="100">
		</isperp:column>
		<isperp:column name="spt_name" title="备件名称" width="150">
		</isperp:column>
		<isperp:column name="spt_model" title="规格型号" width="100">
		</isperp:column>
		<isperp:column name="spt_unit" title="单价" width="60">   
		</isperp:column>
		<isperp:column name="cur_stock_quantity" title="当前库存" width="85">
		</isperp:column>
		<isperp:column name="quantity" title="维修数" width="120" >   
		</isperp:column> 
		<isperp:column name="repair_in_quantity" title="入库数" width="120" editable="true"
					edittype="text" isFormatterFun="true"
					formatter="return getSpInputText(cellvalue, rowObject,3,3);">   
		</isperp:column> 
		<isperp:column name="repair_discard_quantity" title="报废数" width="120" editable="true"
					edittype="text" isFormatterFun="true"
					formatter="return getSpInputText(cellvalue,rowObject,3,4);">   
		</isperp:column> 
		<isperp:column name="memo" title="备注" width="120" editable="true"
					edittype="text" isFormatterFun="true"
					formatter="return getSpInputText(cellvalue,rowObject,1);">   
		</isperp:column>
		
	</jsp:body>
</isperp:grid>
</div>

<isperp:buttonList>
	<isperp:processButtons processKey="sparepartBackProcess"
	    beforeSubmit="function c(){return check();}"
		processId="${model.processId }" businessKey="${model.id }"
		formId="sparepartBackForm"></isperp:processButtons>
</isperp:buttonList>

<script type="text/javascript">
   $(function(){
	   var useProjectId='${model.projectId}';
	   if(!useProjectId)
	   {
	      $("#projectId").val("").trigger("chosen:updated");
	   }
	});
	
	function check()
	{
	   var result=true;
	   var o = jQuery("#sparepartBackDetailGrid");
	   var rows = o.jqGrid('getRowData'); 
	   for(var i=0;i<rows.length;i++)
	   {
	      var q1=parseInt(rows[i].quantity);
	      var q2=$("#i3_"+rows[i].id).val();
	      var q3=$("#i4_"+rows[i].id).val();
	      var q4=parseInt(q2)+parseInt(q3);
	      if(q1!=q4)
	      {
	        result=false;
	        break;
	      }
	   }
	   if(!result)
	   {
	    isperp.alert("数据错误，维修数=入库数+报废数！");
	   }
	   return result;
	}
	
	function afterSelected(row) {
		$.getJSON("project/member/listProjectByUsername?username=" + row[0], function(data) {

			$("#projectId").empty();
			if (data[0]) {
				$(data).each(function(index, item) {
					$("#projectId").append('<option value="'+item.id+'">' + item.name + '</option>');
				});
			}
			$("#projectId").trigger("chosen:updated");
		});
	}
</script>